/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pengeluaran;

import Pemasukan.Pemasukan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import tools.koneksi;

/**
 *
 * @author w7
 */
public class PengeluaranKontrol {

    Connection conn;
    private static PengeluaranKontrol pengeluaranKontrol;

    public PengeluaranKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static PengeluaranKontrol getKoneksiPengeluaran() throws SQLException {
        koneksi k = new koneksi();
        if (pengeluaranKontrol == null) {
            pengeluaranKontrol = new PengeluaranKontrol(k.getDBConnection());
        }
        return pengeluaranKontrol;
    }

    public void tambahPengeluaran(Pengeluaran b) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql1 = "insert into "
                    + " transaksi (TANGGAL, NO_INVOICE, KETERANGAN, KODE_REKENING) values (?,?,?,?)";
            String sql2 = "insert into "
                    + " PENGELUARAN1 (NO_INVOICE, ID_UNIT, JUMLAH_PERMINTAAN_DANA, DETAIL) values (?,?,?,?)";
            statement = (PreparedStatement) conn.prepareStatement(sql1);
            statement.setDate(1, new Date(b.getTanggal().getTime()));
            statement.setString(2, b.getNoInvoice());
            statement.setString(3, b.getKeterangan());
            statement.setString(4, b.getKodeRekening());
            statement.executeUpdate();
            statement = (PreparedStatement) conn.prepareStatement(sql2);
            statement.setString(1, b.getNoInvoice());
            statement.setString(2, b.getIDunit());
            statement.setDouble(3, b.getJumlahMintaDana());
            statement.setString(4, b.getDetail());
            statement.executeUpdate();
            // System.out.println("mjjyi_"+b.getKeterangan());
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

//    public void tambahPengeluaranGaji(Pengeluaran b) throws SQLException {
//        PreparedStatement statement = null;
//        try {
//            conn.setAutoCommit(false);
//            String sql = "insert into "
//                    + " PENGELUARAN1 ( NO_INVOICE,TANGGAL, ID_UNIT, JUMLAH_PERMINTAAN_DANA,KODE_REKENING, KETERANGAN, DETAIL) values (?,?,?,?,?,?,?)";
//            statement = (PreparedStatement) conn.prepareStatement(sql);
//
//            statement.setString(1, b.getNoInvoice());
//            statement.setDate(2, new Date(b.getTanggal().getTime()));
//            statement.setString(3, b.getIDunit());
//            statement.setDouble(4, b.getJumlahMintaDana());
//            statement.setString(5, b.getKodeRekening());
//            statement.setString(6, b.getKeterangan());
//            statement.setString(7, b.getDetail());
//            statement.executeUpdate();
//            conn.commit();
//        } catch (SQLException exception) {
//            conn.rollback();
//            throw exception;
//        } finally {
//            try {
//                conn.setAutoCommit(true);
//                if (statement != null) {
//                    statement.close();
//                }
//            } catch (SQLException exception) {
//                throw exception;
//            }
//        }
//
//    }
//      public Pengeluaran getNamaUnitOtomatis(String ID_UNIT) throws SQLException {
//        PreparedStatement statement = null;
//        ResultSet result = null;
//        Pengeluaran k = null;
//        try {
//            conn.setAutoCommit(false);
//            statement = conn.prepareStatement("select NAMA_UNIT from UNIT where ID_UNIT=?");
//            statement.setString(1, ID_UNIT);
//            result = statement.executeQuery();
//            while (result.next()) {
//                k = new Pengeluaran();
//                k.setNamaUnit(result.getString(1));
//
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(PengeluaranKontrol.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return k;
//    }
}
